package com;


import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

public class BlockQueueTest {

    public static void main(String[] args) {
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(3);
        System.out.println(queue.add("a"));
        System.out.println(queue.add("b"));
        System.out.println(queue.add("v"));
        System.out.println(queue.add("v"));
        System.out.println(queue.remove());
        System.out.println(queue.remove());
        System.out.println(queue.remove());
        queue.offer("a");
        queue.offer("a");
        queue.offer("a");
        queue.offer("a");
        System.out.println(queue.poll());
        BlockingQueue blockingQueue = new SynchronousQueue();
       new Thread(()->{
           try {
               System.out.println("添加了a");
               blockingQueue.put("a");
               System.out.println("添加了b");
               blockingQueue.put("b");
               System.out.println("添加了v");
               blockingQueue.put("v");
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       },"Aa").start();
       new Thread(()->{
           try {
               TimeUnit.SECONDS.sleep(3);
               blockingQueue.take();
               TimeUnit.SECONDS.sleep(3);
               blockingQueue.take();
               TimeUnit.SECONDS.sleep(3);
               blockingQueue.take();
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       },"BB").start();

    }
}

